layui.use(['form','layer','element','laydate'],function(){
	
	var form = layui.form,layer = layui.layer,element=layui.element,laydate = layui.laydate;
	
	laydate.render({elem:'#planDate', range:'到'});
	laydate.render({elem:'#scheduleCron',type:'time'});
	laydate.render({elem:'#scheduleCron1',type:'datetime'});
	
	$("#save-btn").on('click',function(){save();});
	
	form.on('select(scheduleType)',function(data){
		
		var val = data.value;
		if(val == 3){
			$("#splan").show();
		}else
			$("#splan").hide();
	});
	
	form.on('select(scheduleFrequency)',function(data){
		var val = data.value;
		if(val == 1){
			$("#scheduleCron1").show();
			$("#scheduleCron").hide();
		}else if(val == 2){
			$("#scheduleCron").show();
			$("#scheduleCron1").hide();
		}
	});
	
	$("#caseadd-btn").on('click',function(){
		var caseIds = $("#caseIds").val();
		x_admin_show('添加案例','../task/toCaseSelect?caseIds='+caseIds,600,500,true)
	});
	
	function selCase(data){
		$("#casedg").empty();
		var caseIds = [];
		for(var i = 0 ;i < data.length;i++){
			var casedata = data[i];
			$("#casedg").append("<tr><td>"+casedata.caseName+"</td>"+
                    "<td>"+casedata.appName+"</td>"+
					  "<td>"+casedata.caseType+"</td>"+
					  "</tr>");
			caseIds.push(casedata.caseId);
		}
		$("#caseIds").val(caseIds.join(","));
	}
	window.selCase = selCase;
	
	$("#deviceadd-btn").on('click',function(){
		var deviceIds = $("#deviceIds").val();
		x_admin_show('添加设备','../task/toDeviceSelect?deviceIds='+deviceIds,600,500,true)
	});
	function selDevice(data){
		$("#devicedg").empty();
		var deviceIds = [];
		for(var i = 0 ; i < data.length;i++){
			var devicedata = data[i];
			$("#devicedg").append("<tr><td>"+devicedata.serialNumber+"</td>"+
				                      "<td>"+devicedata.platform+"</td>"+
									  "<td>"+devicedata.brand+"</td>"+
									  "<td>"+devicedata.model+"</td>"+
									  "</tr>");
			deviceIds.push(devicedata.deviceId);
		}
		$("#deviceIds").val(deviceIds.join(","));
	}
	window.selDevice = selDevice;
	
	function save(){
		
		var data = {};
		var taskName = $("#taskName").val();
		if(isBlank(taskName)){
			layer.tips("请填写名称","#taskName",{
      			  tips: [1, '#FF0000'],
      			  time: 3000
      			});
			return false;
		}
		data['taskName'] = taskName;
		
		var appId = $("#appId").val();
		data['appId'] = appId;
		
		var testStatus = $("#testStatus").val();
		data['testStatus'] = testStatus;
		
		var planDate = $("#planDate").val();
		/*if(isBlank(planDate)){
			layer.tips("请选择计划日期","#planDate",{
      			  tips: [1, '#FF0000'],
      			  time: 3000
      			});
			return false;
		}*/
		if(!isBlank(planDate)){
			var planStartDate = planDate.split("到")[0].trim();
			data['planStartDateStr'] = planStartDate;
			
			var planEndDate = planDate.split("到")[1].trim();
			data['planEndDateStr'] = planEndDate;
		}
		var testType = $("#testType").val();
		data['testType'] = testType;
		
		var tryNum = $("#tryNum").val();
		data['tryNum'] = tryNum;
		
		var scheduleType = $("#scheduleType").val();
		data['scheduleType'] = scheduleType;
		
		
		if(scheduleType == 3){
			var scheduleFrequency = $("#scheduleFrequency").val();
			data['scheduleFrequency'] = scheduleFrequency;
			
			if(scheduleFrequency == 2){
				var scheduleCron = $("#scheduleCron").val();
				if(isBlank(scheduleCron)){
					layer.tips("请选择执行时间","#scheduleCron",{
						  tips: [1, '#FF0000'],
						  time: 3000
						});
					return false;
				}
				var cron = scheduleCron.split(":");
				var realCron = parseInt(cron[2],10)+" "+parseInt(cron[1],10)+" "+parseInt(cron[0],10)+" * * ?";
				data['scheduleCron'] = realCron;
			}else if(scheduleFrequency == 1){
				var scheduleCron = $("#scheduleCron1").val();
				if(isBlank(scheduleCron)){
					layer.tips("请选择执行时间","#scheduleCron1",{
						  tips: [1, '#FF0000'],
						  time: 3000
						});
					return false;
				}
				var cron = scheduleCron.split(" ");
				var crondate = cron[0].split("-");
				var crontime = cron[1].split(":");
				var realCron = parseInt(crontime[2],10)+" "+parseInt(crontime[1],10)+" "+parseInt(crontime[0],10)+
				               " "+parseInt(crondate[2],10)+" "+parseInt(crondate[1],10)+" ? "+parseInt(crondate[0],10);
				data['scheduleCron'] = realCron;
			}
		}else if(scheduleType == 2){
			var now = new Date();
			var later = now.getTime()+30000;
			var cron = new Date(later);
			var realCron = cron.getSeconds()+" "+cron.getMinutes()+" "+cron.getHours()+
			               " "+cron.getDate()+" "+(parseInt(cron.getMonth(),10)+1)+" ? "+cron.getFullYear();
			data['scheduleCron'] = realCron;
			data['scheduleFrequency'] = 1;
		}
		
		
		
		var caseIds = $("#caseIds").val();
		if(isBlank(caseIds)){
			layer.tips("请选择执行案例","#caseadd-btn",{
      			  tips: [1, '#FF0000'],
      			  time: 3000
      			});
			return false;
		}
		data['taskCaseIds'] = caseIds;
		
		var deviceIds = $("#deviceIds").val();
		if(isBlank(deviceIds)){
			layer.tips("请选择执行设备","#deviceadd-btn",{
      			  tips: [1, '#FF0000'],
      			  time: 3000
      			});
			return false;
		}
		data['taskDeviceIds'] = deviceIds;
		
		
		ajaxPost('../task/saveTask',data,
		function(data){
			var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index);
            parent.initPage();
		},
		function(data){
			parent.layer.alert(data.errorMsg, {
        		icon: 5,
        		title: "错误"
        	});
		});
		
	}
	
});